home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 7 / Amiga Format AFCD07 (Dec 1996, Issue 91).iso / serious / shareware / programming / ixemul-complete / ixemul / library / unlink.c < prev    next >
C/C++ Source or Header  |  1996-05-18  |  3KB  |  101 lines

  1. /*
  2.  *  This file is part of ixemul.library for the Amiga.
  3.  *  Copyright (C) 1991, 1992  Markus M. Wild
  4.  *
  5.  *  This library is free software; you can redistribute it and/or
  6.  *  modify it under the terms of the GNU Library General Public
  7.  *  License as published by the Free Software Foundation; either
  8.  *  version 2 of the License, or (at your option) any later version.
  9.  *
  10.  *  This library is distributed in the hope that it will be useful,
  11.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.  *  Library General Public License for more details.
  14.  *
  15.  *  You should have received a copy of the GNU Library General Public
  16.  *  License along with this library; if not, write to the Free
  17.  *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  *
  19.  *  unlink.c,v 1.1.1.1 1994/04/04 04:30:37 amiga Exp
  20.  *
  21.  *  unlink.c,v
  22.  * Revision 1.1.1.1  1994/04/04  04:30:37  amiga
  23.  * Initial CVS check in.
  24.  *
  25.  *  Revision 1.1  1992/05/14  19:55:40  mwild
  26.  *  Initial revision
  27.  *
  28.  */
  29.  
  30. #define _KERNEL
  31. #include "ixemul.h"
  32. #include "kprintf.h"
  33. #include <string.h>
  34.  
  35. static int
  36. __delete_func (struct StandardPacket *sp, struct MsgPort *handler,
  37.                BPTR parent_lock,
  38.            BSTR name,
  39.            void *dummy, int *no_error)
  40. {
  41.   sp->sp_Pkt.dp_Type = ACTION_DELETE_OBJECT;
  42.   sp->sp_Pkt.dp_Arg1 = parent_lock;
  43.   sp->sp_Pkt.dp_Arg2 = name;
  44.  
  45.   PutPacket (handler, sp);
  46.   __wait_sync_packet (sp);
  47.  
  48.   *no_error = sp->sp_Pkt.dp_Res1 == -1;
  49.  
  50.   /* stop if we failed because of symlink - reference */
  51.   return 0;
  52. }
  53.  
  54. int
  55. unlink (char *name)
  56. {
  57.   int err;
  58.   struct file *fp;
  59.   int result;
  60.   struct stat st;
  61.  
  62.   result = 0;
  63.  
  64.   if (syscall(SYS_stat, name, &st) == 0 && (st.st_mode & S_IWUSR) == 0)
  65.     if (syscall(SYS_chmod, name, st.st_mode | S_IWUSR))
  66.       return -1;
  67.   /* first try to normally delete the file, if we get an
  68.    * 'object in use' error, we'll try another way out.. */
  69.   if (__plock (name, __delete_func, 0)) goto ret;
  70.  
  71.   /* so there was an error.. */
  72.   err = IoErr();
  73.   if (err == ERROR_OBJECT_IN_USE)
  74.     {
  75.       /* check whether the name is an absolute filename, if yes, we
  76.        * can walk thru our filetab to see, whether WE have this file
  77.        * open, and set its unlink-flag, if not, return the error */
  78.  
  79.       /* if we fully and consistent allow Unix-pathnames, we can test
  80.        * for a leading '/' too. */
  81.       if (index(name, ':') || ((ix.ix_flags & ix_translate_slash) && name[1] && name[0] == '/'))
  82.     {
  83.       ix_lock_base();
  84.       for (fp = ix.ix_file_tab; fp < ix.ix_fileNFILE; fp++)
  85.         if (fp->f_count && fp->f_name && !strcmp(fp->f_name, name))
  86.           {
  87.         fp->f_flags |= FUNLINK;
  88.         ix_unlock_base();
  89.         goto ret;
  90.           }
  91.       ix_unlock_base();
  92.     }
  93.     }
  94.   errno = __ioerr_to_errno(err);
  95.   KPRINTF (("&errno = %lx, errno = %ld\n", &errno, errno));
  96.   result = -1;
  97.  
  98. ret:
  99.   return result;
  100. }
  101.